package com.example.sherlockzhong.plush_v2.View;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Utils.ImageUtil;

import java.util.ArrayList;

/**
 * Created by DosonLeung on 2014/9/19.
 */
public class MainContentFrame extends LinearLayout {
    private ArrayList<String> cardId = new ArrayList<String>();

    public MainContentFrame(Context context) {
        super(context);
    }

    public MainContentFrame(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addCard(Activity app, CardHorizontalScrollView v, int icon) {
        super.addView(v);
        cardId.add(v.getCardId());
        Display display = app.getWindowManager().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        final int windowWidth = p.x;
        String Class = v.getCardClass();
        if (Class.equals("Consumption")) {
            ImageUtil.scaleImage(v, R.id.consumption_icon, R.drawable.consume_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.consumption_wallet_icon, R.drawable.coins, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.consumption_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Exam")) {
            ImageUtil.scaleImage(v, R.id.exam_icon, R.drawable.exam_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.exam_flag, R.drawable.flag, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.exam_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Grade")) {
            ImageUtil.scaleImage(v, R.id.grade_icon, R.drawable.grade_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.grade_cap_icon, R.drawable.square_college_cap, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.grade_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Lesson")) {
            ImageUtil.scaleImage(v, R.id.lesson_icon, R.drawable.lesson_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.lesson_bad_icon, R.drawable.bad, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.lesson_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("News")) {
            if (icon != 0)
                ImageUtil.scaleImage(v, R.id.news_icon, icon, windowWidth, 0.3, 0.97);
            else
                ImageUtil.scaleImage(v, R.id.news_icon, R.drawable.unknown_dark, windowWidth, 0.3, 0.97);
            ImageUtil.scaleImage(v, R.id.news_bell, R.drawable.bell, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.news_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Weather")) {
            if (icon != 0)
                ImageUtil.scaleImage(v, R.id.weather_icon, icon, windowWidth, 0.3, 0.97);
            else
                ImageUtil.scaleImage(v, R.id.weather_icon, R.drawable.unknown_dark, windowWidth, 0.3, 0.97);
            ImageUtil.scaleImage(v, R.id.weather_drop, R.drawable.weather_drop, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.weather_pin, R.drawable.pin, windowWidth, 0.05, 1);
        }
    }

    public void addCard(Activity app, CardHorizontalScrollView v) {
        super.addView(v, 0);
        cardId.add(v.getCardId());
        Display display = app.getWindowManager().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        final int windowWidth = p.x;
        String Class = v.getCardClass();
        if (Class.equals("Consumption")) {
            ImageUtil.scaleImage(v, R.id.consumption_icon, R.drawable.consume_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.consumption_wallet_icon, R.drawable.coins, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.consumption_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Exam")) {
            ImageUtil.scaleImage(v, R.id.exam_icon, R.drawable.exam_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.exam_flag, R.drawable.flag, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.exam_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Grade")) {
            ImageUtil.scaleImage(v, R.id.grade_icon, R.drawable.grade_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.grade_cap_icon, R.drawable.square_college_cap, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.grade_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Lesson")) {
            ImageUtil.scaleImage(v, R.id.lesson_icon, R.drawable.lesson_icon, windowWidth, 0.35, 0.97);
            ImageUtil.scaleImage(v, R.id.lesson_bad_icon, R.drawable.bad, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.lesson_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("News")) {
            ImageUtil.scaleImage(v, R.id.news_icon, R.drawable.unknown_dark, windowWidth, 0.3, 0.97);
            ImageUtil.scaleImage(v, R.id.news_bell, R.drawable.bell, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.news_pin, R.drawable.pin, windowWidth, 0.05, 1);
        } else if (Class.equals("Weather")) {
            ImageUtil.scaleImage(v, R.id.weather_icon, R.drawable.unknown_dark, windowWidth, 0.3, 0.97);
            ImageUtil.scaleImage(v, R.id.weather_drop, R.drawable.weather_drop, windowWidth, 0.06, 1);
            ImageUtil.scaleImage(v, R.id.weather_pin, R.drawable.pin, windowWidth, 0.05, 1);
        }
    }

    public int removeView(BasicCardView v) {
        String id = v.getCardId();
        int index = cardId.indexOf(id);
        if (index != -1) {
            cardId.remove(index);
            super.removeView(v);
            return index;
        } else {
            return 0;
        }
    }

    public int getCardIndex(BasicCardView v) {
        String cardID = v.getCardId();
        return cardId.indexOf(cardID);
    }

    public void removeAllViews() {
        cardId.clear();
        while (getChildCount() > 0) {
            super.removeViewAt(getChildCount() - 1);
        }
    }

    private void cardFollow(BasicCardView v, int index) {
        View sibling1;
        View sibling2;
        View sibling3;
        if (this.getChildAt(index + 1) != null) {
            sibling1 = this.getChildAt(index + 1);
            animation(sibling1, sibling1.getTop());
        }
        if (this.getChildAt(index + 2) != null) {
            sibling2 = this.getChildAt(index + 2);
            animation(sibling2, sibling2.getTop());
        }
        if (this.getChildAt(index + 3) != null) {
            sibling3 = this.getChildAt(index + 3);
            animation(sibling3, sibling3.getTop());
        }
    }

    public void animation(View inner, int top) {
        // 开启移动动画
        TranslateAnimation ta = new TranslateAnimation(0, 0, top, top * 2);
        ta.setDuration(200);
        inner.startAnimation(ta);
        // 设置回到正常的布局位置
        inner.layout(inner.getLeft(), top * 2, inner.getRight(), top);
    }
}
